

import java.awt.Graphics2D;
import java.util.ArrayList;

public class Entity
{
	SpriteImage sprite;
	protected int x;
	protected int y;
	boolean flip = false;
	Game game;
	ArrayList<Entity> adjacentTiles;
	
	public Entity(Game game, int x, int y, SpriteImage sprite)
	{
		this.game = game;
		this.x = x;
		this.y = y;
		this.sprite = sprite;
		
	}

	public void draw(Graphics2D g)
	{
		sprite.draw(g, x, y); 
		//g.drawRect(x, y, sprite.frameWidth, sprite.height);
		
	}
	public void update(long gameTime)
	{
		
	}
	
	
	public int[] getBounds()
	{
		int[] bounds = new int[4];
		
		//left
		bounds[0] = x;
		//right
		bounds[1] = x + sprite.frameWidth;
		
		//top
		bounds[2] = y;
		//bottom
		bounds[3] = y + sprite.height;
		return bounds; 
	}
	public boolean collidesWith(Entity e)
	{
		int e1[] = getBounds();
		int e2[] = e.getBounds();
		
		if (e1[0] > e2[1] || e1[1] < e2[0] || e1[2] > e2[3] || e1[3] < e2[2] )
		{
			return false;
		}
		return true;
	}
	
	public boolean collidesLeft(Entity e)
	{
		int[] e1 = getBounds();
		int[] e2 = e.getBounds();
		//check y bounds
		if ((e1[3] > e2[2] || e1[3] < e2[2]) && collidesWith(e))
		{
			if(e1[0] <= e2[1] && e1[1] > e2[1])
			{
				return true;
			}
		}
		return false;
	}
	public boolean collidesRight(Entity e)
	{
		int[] e1 = getBounds();
		int[] e2 = e.getBounds();
		//check y bounds
		if ((e1[3] > e2[2] || e1[3] < e2[2]) && collidesWith(e))
		{
			if(e1[1] >= e2[0] && e1[0] < e2[0])
			{
				return true;
			}
		}
		return false;
	}
	public boolean collidesTop(Entity e)
	{
		int[] e1 = getBounds();
		int[] e2 = e.getBounds();
		//check y bounds
		if ((e1[1] > e2[0] || e1[1] < e2[0]) && collidesWith(e))
		{
			if(e1[2] <= e2[3] && e1[3] > e2[3])
			{
				return true;
			}
		}
		return false;
	}
	public boolean collidesBottom(Entity e)
	{
		int[] e1 = getBounds();
		int[] e2 = e.getBounds();
		//check y bounds
		if ((e1[1] > e2[0] || e1[1] < e2[0]) && collidesWith(e))
		{
			if(e1[3] >= e2[2] && e1[2] < e2[2])
			{
				return true;
			}
		}
		return false;
	}	
	
	public ArrayList<Entity> findAdjacentTiles()
	{
		adjacentTiles = new ArrayList<Entity>();
		//find left bound - needs to be a multiple of the tileSize
		int leftBound = x - (x % MapReader.tileWidth);
		
		//find right bound
		int rightBound = (x + sprite.frameWidth) + (x + sprite.frameWidth) % MapReader.tileWidth;
		
		//find upper bound
		int upperBound = y - (y % MapReader.tileHeight);
		
		//find lower bound
		int lowerBound = (y + sprite.height) + (y + sprite.height) % MapReader.tileHeight;
		Entity toAdd;
		for(int y = upperBound; y <= lowerBound; y += MapReader.tileWidth)
		{
			for(int x = leftBound; x <= rightBound; x += MapReader.tileWidth)
			{
				
				toAdd = game.tileMap.get(new BlockLocation(x, y));
				if (toAdd != null)
				{
					adjacentTiles.add(game.tileMap.get(new BlockLocation(x, y)));
				}
			}
			
		}
		return adjacentTiles;
	}


}
